package com.zqxx.examine.portal.controller.action;


import java.io.IOException;

import org.springframework.amqp.AmqpException;
import org.springframework.amqp.core.AmqpTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.gson.Gson;
import com.zqxx.examine.model.exam.AnswerSheet;
import com.zqxx.examine.model.exam.Exam;
import com.zqxx.examine.model.exam.ExamHistory;
import com.zqxx.examine.model.exam.Message;
import com.zqxx.examine.portal.security.UserInfo;
import com.zqxx.examine.portal.service.ExamService;

@Controller
public class ExamAction
{
  @Autowired
  private ExamService examService;
  @Autowired
  private AmqpTemplate qmqpTemplate;
  
  @RequestMapping(value={"student/get-answersheet/{histId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public AnswerSheet getAnswerSheet(@PathVariable("histId") int histId)
  {
    ExamHistory history = this.examService.getUserExamHistListByHistId(histId);
    Gson gson = new Gson();
    AnswerSheet answerSheet = (AnswerSheet)gson.fromJson(history.getAnswerSheet(), AnswerSheet.class);
    return answerSheet;
  }
  
  @RequestMapping(value={"student/exam/send-apply/{examId}"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public Message sendApply(@PathVariable("examId") int examId)
  {
    Message msg = new Message();
    UserInfo userInfo = (UserInfo)SecurityContextHolder.getContext().getAuthentication().getPrincipal();
    try
    {
      Exam exam = checkExam(examId);
      
      this.examService.addUserExamHist(userInfo.getUserid(), examId, exam.getExamPaperId(), 0);
    }
    catch (Exception e)
    {
      msg.setResult(e.getMessage());
      e.printStackTrace();
    }
    return msg;
  }
  
  public Exam checkExam(int examId)
    throws Exception
  {
    Exam exam = this.examService.getExamById(examId);
    if (exam == null) {
      throw new Exception("考试不存在！或已经申请过一次！");
    }
    if (exam.getApproved() != 1) {
      throw new Exception("不能申请一个未审核通过的考试！");
    }
    return exam;
  }
  
  @RequestMapping(value={"/student/exam-submit"}, method={org.springframework.web.bind.annotation.RequestMethod.POST})
  @ResponseBody
  public Message finishExam(@RequestBody AnswerSheet answerSheet)
  {
    Message message = new Message();
    ObjectMapper om = new ObjectMapper();
    try
    {
      this.qmqpTemplate.convertAndSend("examplus.answersheetToScoreMarker", om.writeValueAsBytes(answerSheet));
    }
    catch (AmqpException e)
    {
      e.printStackTrace();
      message.setResult("交卷失败");
      message.setMessageInfo(e.toString());
    }
    catch (com.fasterxml.jackson.core.JsonProcessingException e)
    {
      e.printStackTrace();
      message.setResult("交卷失败");
      message.setMessageInfo(e.toString());
    }
    return message;
  }
  
  @RequestMapping(value={"addAnswerSheet4Test"}, method={org.springframework.web.bind.annotation.RequestMethod.GET})
  @ResponseBody
  public Message addAnswerSheet4Test(Model model)
    throws  IOException
  {
    Message msg = new Message();
    AnswerSheet as = new AnswerSheet();
    as.setExamPaperId(2);
    ObjectMapper om = new ObjectMapper();
    this.qmqpTemplate.convertAndSend("examplus.answersheetToScoreMarker", om.writeValueAsBytes(as));
    return msg;
  }
}
